package at.horario.web.component;

import at.horario.model.Tooltip;
import at.horario.service.GenericManager;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import wicket.contrib.mootools.plugins.MFXTips;

public abstract class TooltipField extends FormComponentPanel {
    Label tooltipLabel = new Label("tooltip", "<img src=\"images/ctx_question_inactive.gif\" onmouseover=\"this.src ='images/ctx_question_active.gif'\" onmouseout=\" this.src ='images/ctx_question_inactive.gif'\"/>");
    @SpringBean(name = "tooltipManager")
    private GenericManager<Tooltip, String> tooltipManager;
    Tooltip tooltip;


    abstract public boolean isLabelVisible();


    public TooltipField(String id) {
        super(id, new Model());
        tooltipLabel.setEscapeModelStrings(false);
        tooltip = new Tooltip();
        String nome = StringUtils.substringAfter(id, "_");
        String descricao = tooltipManager.exists(nome) ? tooltipManager.get(nome).getDescricao() : ".";
        tooltip.setId(nome);
        tooltip.setDescricao(descricao);
        tooltipLabel.add(new MFXTips(descricao));
        add(tooltipLabel);
        add(new HorarioAjaxEditableLabel("descricao", new PropertyModel(tooltip, "descricao")) {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                tooltipManager.saveOrUpdate(tooltip);
                super.onSubmit(target);
            }

            @Override
            public boolean isVisible() {
                return isLabelVisible();
            }
        }
        );

        setOutputMarkupId(true);
    }
}